OpenRoads Designer CONNECT Edition SDK Help

Update drawing and sheet model on change in named boundary

The below code snippet shows changing the graphical element of first named boundary from DGN file. The graphical element is replaced with the new shape element created using alignment. The alignment named as "SampleAlignment" is for demo purpose only. The change in Default model replaces all its reference elements from drawing and sheet model.


//Required References
using System;
using Bentley.DgnPlatformNET;
using Bentley.MstnPlatformNET;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.GeometryNET;
using Bentley.DgnPlatformNET.Elements;
using System.Collections.Generic;
using System.Diagnostics;
using Bentley.CifNET.LinearGeometry;

 public void UpdateSheetAndDrawingOnChangeInNamedBoundary()
        {
            try
            {
                DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
                Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit con = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();

                ShapeElement nbShape = null;
                GeometricModel geomModel = con.GetActiveGeometricModel();
                foreach (Alignment alignment in geomModel.Alignments)
                {
                    //Create new shape element using alignment for updating named boundary shape
                    //Alignment Name "SampleAlignment" is for demonstration purpose only
                    if (alignment.Name == "SampleAlignment")
                    {
                        LinearPoint point1 = alignment.LinearGeometry.GetPointAtDistanceOffset(0, 200);
                        LinearPoint point2 = alignment.LinearGeometry.GetPointAtDistanceOffset(0, -200);
                        LinearPoint point3 = alignment.LinearGeometry.GetPointAtDistanceOffset(100, -200);
                        LinearPoint point4 = alignment.LinearGeometry.GetPointAtDistanceOffset(100, 200);
                        LinearPoint point5 = point1;

                        //User might need unit conversion hereon points based on Unit settings in DGN file
                        DPoint3d[] nbPoints = new DPoint3d[5];
                        nbPoints[0] = point1.Coordinates;
                        nbPoints[1] = point2.Coordinates;
                        nbPoints[2] = point3.Coordinates;
                        nbPoints[3] = point4.Coordinates;
                        nbPoints[4] = point5.Coordinates;

                        //Create new Shape Element from points
                        nbShape = new ShapeElement(dgnModel, null, nbPoints);

                        //Add newly created shape to the Active Dgn Model
                        nbShape.AddToModel();
                        break;
                    }
                }


                //Update named boundary graphical element with new shape element and update the sheet and drawing models
                //Get Named Boundary Collection using DgnModel
                NamedBoundaryCollection namedBoundaries = new NamedBoundaryCollection(dgnModel);
                IEnumerator<NamedBoundary> namedBoundaryIterator = namedBoundaries.GetEnumerator();

                //Update first named boundary from the named boundary list 
                while (namedBoundaryIterator.MoveNext())
                {
                    NamedBoundary namedBoundaryToUpdate = namedBoundaryIterator.Current;
                    if (namedBoundaryToUpdate != null)
                    {
                        //Update named boundary shape with newly created shape element and all its references in Drawing and sheet model
                        nbShape.ReplaceInModel(namedBoundaryToUpdate.GraphicalElement);

                        //Save Named Boundary
                        if (StatusInt.Success != namedBoundaryToUpdate.Save())
                            return;

                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }